﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.IO;
using System.Web;
using System.Net.Mail;
using MailKit.Net.Smtp;
using MimeKit;

namespace Common
{
    public class DTMail
    {
        #region 发送电子邮件
        /// <summary>
        /// 发送电子邮件
        /// </summary>
        /// <param name="smtpserver">SMTP服务器</param>
        /// <param name="enablessl">是否启用SSL加密</param>
        /// <param name="userName">登录帐号</param>
        /// <param name="pwd">登录密码</param>
        /// <param name="nickName">发件人昵称</param>
        /// <param name="strfrom">发件人</param>
        /// <param name="strto">收件人</param>
        /// <param name="subj">主题</param>
        /// <param name="bodys">内容</param>
        public static void sendMail(string smtpserver, int enablessl, string userName, string pwd, string nickName, string strfrom, string strto, string subj, string bodys)
        {
            System.Net.Mail.SmtpClient _smtpClient = new System.Net.Mail.SmtpClient();
            _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
            _smtpClient.Host = smtpserver;//指定SMTP服务器
            _smtpClient.Credentials = new System.Net.NetworkCredential(userName, pwd);//用户名和密码
            if (enablessl == 1)
            {
                _smtpClient.EnableSsl = true;
            }

            MailAddress _from = new MailAddress(strfrom, nickName);
            MailAddress _to = new MailAddress(strto);
            MailMessage _mailMessage = new MailMessage(_from, _to);
            _mailMessage.Subject = subj;//主题
            _mailMessage.Body = bodys;//内容
            _mailMessage.BodyEncoding = System.Text.Encoding.Default;//正文编码
            _mailMessage.IsBodyHtml = true;//设置为HTML格式
            _mailMessage.Priority = MailPriority.Normal;//优先级
            _smtpClient.Send(_mailMessage);
        }
        #endregion

        /// <summary>
        /// 发送邮件(阿里云平台)
        /// </summary>
        /// <param name="bodyStr">邮件内容</param>
        /// <param name="title">邮件标题</param>
        /// <param name="tomail">邮件接收人</param>
        /// <returns>返回结果，成功与否</returns>
        public static string AliyunSendMail(string bodyStr, string title,string tomail)
        {
            System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
            smtpClient.EnableSsl = true;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.Host = "smtpdm.aliyun.com";
            smtpClient.Port = 465;//改用587端口
            System.Net.NetworkCredential nc = new System.Net.NetworkCredential("admin@mail.jinjiajiankang.com", "JJJKadmin16888");
            smtpClient.Credentials = nc.GetCredential(smtpClient.Host, smtpClient.Port, "NTLM");
            //smtpClient.Credentials = new System.Net.NetworkCredential("admin@mail.jinjiajiankang.com", "JJJKadmin16888");
            MailMessage mailMessage = new MailMessage();
            mailMessage.Subject = title;//邮件标题 
            mailMessage.Body = bodyStr;//邮件内容 
            mailMessage.BodyEncoding = System.Text.Encoding.Default;//正文编码  
            mailMessage.IsBodyHtml = true;//设置为HTML格式  
            mailMessage.Priority = MailPriority.High;//优先级  
            mailMessage.From = new MailAddress("admin@mail.jinjiajiankang.com");
            mailMessage.To.Add(tomail);
            try
            {
                smtpClient.Send(mailMessage);
                return "发送成功";
            }
            catch (Exception e)
            {
                return "发送失败" + e.InnerException.Message;
            }
        }

        /// <summary>
        /// 发送邮件SSL
        /// </summary>
        /// <param name="smtpserver">SMTP服务器</param>
        /// <param name="smtppost">SMTPPOST(网易、QQ支持 25[未加密]，465和587[SSL加密])</param>
        /// <param name="enablessl">是否启用SSL加密</param>
        /// <param name="userName">登录帐号</param>
        /// <param name="pwd">登录密码</param>
        /// <param name="nickName">发件人昵称</param>
        /// <param name="strfrom">发件人邮箱</param>
        /// <param name="tomail">收件人邮箱</param>
        /// <param name="title">主题</param>
        /// <param name="bodyStr">内容</param>
        /// <returns></returns>
        public static string SendEmailSSL(string smtpserver, int smtppost, bool enablessl, string userName,string pwd,string nickName,string strfrom, string tomail,string title, string bodyStr)
        {
            var message = new MimeMessage();
            message.From.Add(new MailboxAddress(nickName, strfrom));
            message.To.Add(new MailboxAddress(strfrom, tomail));
            message.Subject = title; //邮件标题
            var builder = new BodyBuilder
            {
                HtmlBody = bodyStr
            };
            message.Body = builder.ToMessageBody();
            using (var client = new MailKit.Net.Smtp.SmtpClient())
            {
                client.ServerCertificateValidationCallback = (s, c, h, e) => true;

                var mSendMail = userName;
                var mSendPwd = pwd;
                client.Connect(smtpserver, smtppost, enablessl);
                client.Authenticate(mSendMail, mSendPwd);
                try
                {
                    client.Send(message);//发送邮件
                    client.Disconnect(true);
                    return "发送成功";
                }
                catch (SmtpCommandException ex)
                {
                    return "发送失败" + ex.Message;
                }
                catch (Exception ex)
                {
                    return "发送失败" + ex.Message;
                }
            }

        }
    }
}
